
from qiniu import Auth, put_file, etag, BucketManager
import qiniu.config
def handle_uploaded_file(f,fileName,filePath):
    # 打开文件路径filePath以二进制写模式('wb')，准备写入数据
    # 'with' 语句用于管理文件的打开和关闭，确保在操作完成后文件被
    with open(filePath, 'wb') as destination:
        # 遍历文件对象f的每个数据块，'chunks'方法用于处理大
        for chunk in f.chunks():
            # 将数据块写入到目标文件destination中
            destination.write(chunk)


    # 需要填写你的 Access Key 和 Secret Key
    access_key = 'rSxXXcSmNiLzqRItRbcRkigZWaJRDNBu5LZEoRHZ'
    secret_key = 'AL4jCtxTDpOILsvkFlWCO1eerLnWwkIu943Rhm5k'
    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    # 要上传的空间
    bucket_name = 'ruici-wx'
    # 上传后保存的文件名
    key = fileName
    # 生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)
    # 要上传文件的本地路径
    localfile = filePath
    ret, info = put_file(token, key, localfile, version='v2')
    print(info)
    assert ret['key'] == key
    assert ret['hash'] == etag(localfile)

def handle_delet_file(fileName):
    # 需要填写你的 Access Key 和 Secret Key
    access_key = 'rSxXXcSmNiLzqRItRbcRkigZWaJRDNBu5LZEoRHZ'
    secret_key = 'AL4jCtxTDpOILsvkFlWCO1eerLnWwkIu943Rhm5k'
    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    # 初始化BucketManager
    bucket = BucketManager(q)
    # 要上传的空间
    bucket_name = 'ruici-wx'
    key='fileName'


    # 删除bucket_name 中的文件 key
    ret, info = bucket.delete(bucket_name, key)
    print('删除文件')
    print(info)

"""
def handle_clear_file(fileName):
    from qiniu import BucketManager
    q = Auth("DznSh30ojvg3A2QAGlHgshZXTWHBPzUtG-vPyogA", "qsJgDTB0UmkzjI-MlMRm7pbJoLuMLMMthmgSLLXo")
    # 初始化BucketManager
    bucket = BucketManager(q)
    # 要上传的空间
    bucket_name = 'zhikengruige'
    key = fileName
    # 删除bucket_name 中的文件 key
    ret, info = bucket.delete(bucket_name, key)
    print(info)
    assert ret == {}
"""